package by.gravity.common.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import by.gravity.common.utils.ContextHolder;

public class PreferenceHelper {

	public static String getString(int resId) {
		return getContext().getString(resId);
	}

	public static String getString(int resId, int defResId) {
		return getString(resId, getContext().getString(defResId));
	}

	public static String getString(int resId, String def) {
		return getString(getContext().getString(resId), def);
	}

	public static String getString(String key, String def) {

		return getSharedPreferences().getString(key, def);
	}

	public static void putString(int resId, String value) {
		putString(getContext().getString(resId), value);
	}

	public static void putString(int resId, int valueResId) {
		putString(getContext().getString(resId), getContext().getString(valueResId));
	}

	public static void putString(String key, String value) {
		getEditor().putString(key, value).commit();

	}

	public static void putInt(int resId, int value) {
		getEditor().putInt(getContext().getString(resId), value).commit();
	}

	public static int getInt(int key, int defValue) {
		return getSharedPreferences().getInt(getContext().getString(key), defValue);
	}

	public static int getInt(String key, int defValue) {
		return getSharedPreferences().getInt(key, defValue);
	}

	public static long getLong(int key, long defValue) {
		return getLong(getString(key), defValue);
	}

	public static long getLong(String key, long defValue) {
		return getSharedPreferences().getLong(key, defValue);
	}

	public static void putLong(String key, long value) {
		getEditor().putLong(key, value).commit();
	}

	public static void putLong(int key, long value) {
		putLong(getString(key), value);
	}

	private static Editor getEditor() {
		return PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
	}

	private static SharedPreferences getSharedPreferences() {
		return PreferenceManager.getDefaultSharedPreferences(getContext());

	}

	private static Context getContext() {
		return ContextHolder.getContext();
	}

}
